package com.qf.service.impl;

import com.qf.config.RedisKey;
import com.qf.dao.mapper.QfCourseDao;
import com.qf.pojo.dto.LiveUser;
import com.qf.pojo.dto.QfCourse;
import com.qf.pojo.reqVo.CourseReq;
import com.qf.pojo.resVo.BaseResult;
import com.qf.service.ShopCardService;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Service
public class ShopCardServiceImpl implements ShopCardService {

    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    QfCourseDao qfCourseDao;
    @Override
    public BaseResult addShopCard(CourseReq courseReq, HttpServletRequest request) {
        //如果复用 findUserByToken 方法获取到的时一个0 则代表用户的登录出现了问题，直接返回即可
        BaseResult userByToken = userService.findUserByToken(request);
        if (userByToken.getCode()==0){
            return userByToken;
        }
        //如果code为1 代表用户的登录是OK的，我们进行购物车的操作，获取到登录的当前用户的详情信息
        LiveUser user = (LiveUser)userByToken.getData();
        //该用户的购物车key
        String shopKey = RedisKey.SHOP_CARD + user.getUserId().toString();
        //判断当前的课程是否已经在购物车中存在，如果存在，则告诉用户，购物车已添加
        Object couse = redisTemplate.opsForHash().get(shopKey, courseReq.getCourseid().toString());
        if (couse==null) {
            //1.用前台传入的courseid查询课程的详细信息
            QfCourse qfCourse = qfCourseDao.queryById(courseReq.getCourseid());
            //继续向购物车中添加课程信息
            redisTemplate.opsForHash().put(shopKey, qfCourse.getId().toString(), qfCourse);
            return new BaseResult(1, "添加成功！", null, null);
        }
        return new BaseResult(0,"课程已存在，不允许重复添加",null,null);
    }

    @Override
    public BaseResult findShopCardByToken(HttpServletRequest request) {

        //如果复用 findUserByToken 方法获取到的时一个0 则代表用户的登录出现了问题，直接返回即可
        BaseResult userByToken = userService.findUserByToken(request);
        if (userByToken.getCode()==0){
            return userByToken;
        }
        //如果code为1 代表用户的登录是OK的，我们进行购物车的操作，获取到登录的当前用户的详情信息
        LiveUser user = (LiveUser)userByToken.getData();
        //判断redis是否有购物车
        //该用户的购物车key
        String shopKey = RedisKey.SHOP_CARD + user.getUserId().toString();
        Boolean aBoolean = redisTemplate.hasKey(shopKey);
        if (!aBoolean){
           return new BaseResult(0,"当前用户购物车为空!请先添加课程！",null,null);
        }
        Map courselist = redisTemplate.opsForHash().entries(shopKey);
        return new BaseResult(1,"查询成功！",courselist,null);
    }

    @Override
    public BaseResult delShop(CourseReq courseReq, HttpServletRequest request) {
        //如果复用 findUserByToken 方法获取到的时一个0 则代表用户的登录出现了问题，直接返回即可
        BaseResult userByToken = userService.findUserByToken(request);
        if (userByToken.getCode()==0){
            return userByToken;
        }
        //如果code为1 代表用户的登录是OK的，我们进行购物车的操作，获取到登录的当前用户的详情信息
        LiveUser user = (LiveUser)userByToken.getData();
        String shopKey = RedisKey.SHOP_CARD + user.getUserId().toString();
       //删除购物车
        Boolean aBoolean = redisTemplate.hasKey(shopKey);
        if (aBoolean){
            //1.购物车中是否有多个课程，删除掉其中的一个即可
            Long size = redisTemplate.opsForHash().size(shopKey);
            if (size>1){
                //删除掉某个课程信息
                redisTemplate.opsForHash().delete(shopKey,courseReq.getCourseid().toString());
            }else {
                //2.是否是最后一个课程，我们需要将整个用户的购物车进行删除
                redisTemplate.delete(shopKey);
            }

        }
        return new BaseResult(1,"移除成功！",null,null);
    }
}
